/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the koala4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @Author: GuoYang
 */
package com.koalacloud.koala.zhaikucloud.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koalacloud.koala.zhaikucloud.entity.MallProductType;
import com.koalacloud.koala.zhaikucloud.mapper.MallProductTypeMapper;
import com.koalacloud.koala.zhaikucloud.service.MallProductTypeService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 商品类目
 *
 * @Author: GuoYang
 * @date 2022-02-21 20:14:41
 */
@Service
public class MallProductTypeServiceImpl extends ServiceImpl<MallProductTypeMapper, MallProductType> implements MallProductTypeService {

    @Override
    public List<Tree<Integer>> getTree(boolean lazy, Integer parentId) {
        if (!lazy) {
            List<TreeNode<Integer>> collect = baseMapper
                    .selectList(Wrappers.<MallProductType>lambdaQuery().orderByAsc(MallProductType::getSortOrder)).stream()
                    .map(getNodeFunction()).collect(Collectors.toList());

            return TreeUtil.build(collect, -1);
        }

        Integer parent = parentId == null ? -1 : parentId;

        List<TreeNode<Integer>> collect = baseMapper
                .selectList(Wrappers.<MallProductType>lambdaQuery().orderByAsc(MallProductType::getSortOrder))
                .stream().map(getNodeFunction()).collect(Collectors.toList());

        return TreeUtil.build(collect, parent);
    }

    private Function<MallProductType, TreeNode<Integer>> getNodeFunction() {
        return menu -> {
            TreeNode<Integer> node = new TreeNode<>();
            node.setId(menu.getId());
            node.setName(menu.getName());
            node.setParentId(menu.getParentId());
            node.setWeight(menu.getSortOrder());
            // 扩展属性
            Map<String, Object> extra = new HashMap<>();
            extra.put("type", menu.getStatus());
            extra.put("label", menu.getName());
            extra.put("status", menu.getStatus());
            extra.put("sortOrder", menu.getSortOrder());
            extra.put("remark", menu.getRemark());
            node.setExtra(extra);
            return node;
        };
    }
}
